import SwiftUI
import SwiftData

// RealNamePage
// 实名信息修改页面

struct RealNamePage: View {
    @Environment(\.modelContext) private var modelContext
    @Query private var loginInfo: [LoginInfo]
    @Query private var userInfo: [UserInfo]
    
    @Environment(\.dismiss) private var dismiss
    
    @State private var realStudentNumberInput: String = ""
    @State private var realNameInput: String = ""
    @State private var cardImageURL: URL?
    
    @State private var showRealnamePicker: Bool = false
    
    @StateObject private var loginMoodifyUserInfoRequester = LoginMoodifyUserInfoRequester()
    
    var body: some View {
        if let lastuserInfo = userInfo.last {
            VStack {
                Image(lastuserInfo.realNameStage == 1 ? "Waiting" :
                      lastuserInfo.realNameStage == 2 ? "Trust" :
                      "Untrust")
                    .resizable()
                    .scaledToFit()
                    .frame(width: 150, height: 150)
                    .padding(.vertical, 20)
                
                Text(lastuserInfo.realNameStage == 1 ? "请等待实名认证" :
                      lastuserInfo.realNameStage == 2 ? "已完成实名认证" :
                      "未进行实名认证")
                    .fontWeight(.light)
                    .font(.largeTitle)
                    .padding(.bottom, 20)
                
                List {
                    Section {
                        HStack {
                            Text("学号")
                            Spacer()
                            TextField("学号", text: $realStudentNumberInput)
                                .multilineTextAlignment(.trailing)
                                .disabled(lastuserInfo.realNameStage != 0)
                        }
                        
                        HStack {
                            Text("姓名")
                            Spacer()
                            TextField("姓名", text: $realNameInput)
                                .multilineTextAlignment(.trailing)
                                .disabled(lastuserInfo.realNameStage != 0)
                        }
                    }

                    if(lastuserInfo.realNameStage == 0) {
                        Section {
                            HStack {
                                Button {
                                    showRealnamePicker.toggle()
                                } label: {
                                    Text("上传学生卡照片")
                                }
                                Spacer()
                            }
                            
                            AsyncImage(url: cardImageURL) { image in
                                image.image?
                                    .resizable()
                                    .scaledToFit()
                            }
                        }
                    }
                }
            }
            .navigationTitle("实名认证")
            .navigationBarTitleDisplayMode(.inline)
            .background(Color(.systemGroupedBackground))
            .onAppear(perform: {
                realStudentNumberInput = lastuserInfo.realStudentNumber
                realNameInput = lastuserInfo.realName
            })
            .sheet(isPresented: $showRealnamePicker) {
                RealName_Picker(imageURL: $cardImageURL)
            }
            .toolbar {
                if(lastuserInfo.realNameStage == 0) {
                    Button {
                        loginMoodifyUserInfoRequester.request_realname(userId: loginInfo.last?.userId ?? "",
                                                                       realNameStage: 1,
                                                                       realStudentNumber: realStudentNumberInput,
                                                                       realName: realNameInput)
                    } label: {
                        Text("完成")
                    }
                }
            }
            .onChange(of: loginMoodifyUserInfoRequester.requestStatus) { oldValue, newValue in
                if(oldValue == .requesting && newValue == .finish) {
                    if(loginMoodifyUserInfoRequester.publishedResult) {
                        dismiss()
                    }
                }
            }
        } else {
            Text("Error")
                .font(.largeTitle)
                .foregroundColor(.red)
        }
    }
}

#Preview {
    RealNamePage()
}
